package org.mch.func.file.content;

import java.io.File;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.apache.commons.io.FileUtils;

public class ContentRegexRep {

	public ContentRegexRep() {
		super();
	}

	public void regexRep(File file, String patternText, String repAfterText) throws Exception {
		String encoding = "utf-8";
		Pattern pattern = Pattern.compile(patternText);
		Matcher matcher = pattern.matcher(FileUtils.readFileToString(file, encoding));
		String result = matcher.replaceAll(repAfterText);

		FileUtils.writeStringToFile(file, result, encoding);
	}

	public static void main(String[] args) throws Exception {
		String patternText = FileUtils.readFileToString(new File("D:/Temp/regex.txt"), "utf-8");
		String repAfterText = FileUtils.readFileToString(new File("D:/Temp/reptext.txt"), "utf-8");
//		new ContentRegexRep().regexRep(new File("E:\\ZL\\htdocs\\Web\\mchweb.net.tool\\api\\python\\python36\\python3.6-att-dictionary-clear.html"), patternText, repText);
		
		File file = new File("E:\\ZL\\htdocs\\Web\\mchweb.net.cdn\\zmw\\css\\stylerun.css");
		String encoding = "utf-8";
		Pattern pattern = Pattern.compile(patternText);
		
		String input = FileUtils.readFileToString(file, encoding);
//		System.out.println(input);
		
		Matcher matcher = pattern.matcher(input);

		while(matcher.find()){  
			int count =matcher.groupCount();
//		    for(int i=0; i<=count; i++){  
		        System.out.println(matcher.group(0));  
//		    }  
		}  

	}
}
